//
//  KSMapItem.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/1/16.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
 
class KSMapItem: KSFormHItem {
    
    private let baiduMapManager = BaseBaiduMapHandler()

    convenience init(appearance: KSFormBaseAppear? = KSFormBaseAppear()) {
        self.init(title: "", appearance: appearance,type:.web)
        if isBaiduMapPlatform() {
            let mapView = baiduMapManager.getMapView(zoomLevel: 14)
            baiduMapManager.mapIsScrollEnabled(flag: false)
            self.addSubview(mapView)
            mapView.snp.makeConstraints { make in
                make.right.equalTo(self).offset(-15)
                make.left.equalTo(self).offset(15)
                make.top.equalTo(self).offset(3)
                make.bottom.equalTo(self).offset(-3)
            }
        } else {
            let mapView = BaseGaodeMapHandler.shared.getMapView(reInit: true, zoomLevel: 14)
            BaseGaodeMapHandler.shared.mapIsScrollEnabled(flag: false)
            self.addSubview(mapView)
            mapView.snp.makeConstraints { make in
                make.right.equalTo(self).offset(-15)
                make.left.equalTo(self).offset(15)
                make.top.equalTo(self).offset(3)
                make.bottom.equalTo(self).offset(-3)
            }
        }
    }
    
    func setCoordinate(lon:String,lat:String) {
        var coor = CLLocationCoordinate2D()
        coor.latitude = doubleTran(lat)
        coor.longitude = doubleTran(lon)
        
        if isBaiduMapPlatform() {
            baiduMapManager.setCenterCoordinate(coor)
            baiduMapManager.startLocation()
            baiduMapManager.removeAnnotations()
            let annotation1 = BMKPointAnnotation()
            annotation1.coordinate = coor
            baiduMapManager.addAnnotation(annotation1)
        } else {
            BaseGaodeMapHandler.shared.setCenterCoordinate(coor)
            BaseGaodeMapHandler.shared.removeAnnotations()
            let annotation1 = MAPointAnnotation()
            annotation1.coordinate = coor
            BaseGaodeMapHandler.shared.addAnnotation(annotation1)
        }
    }
}
